<template>
  <div class="table-form-header">
    <el-form
      ref="listQuery"
      :model="listQuery"
      :rules="rules"
      class="form-container"
    >
      <div class="createPost-main-container">
        <el-row>
          <el-col :span="24">
            <div class="postInfo-container">
              <el-row>
                <el-col
                  v-for="item in dataObject.dataComponent"
                  :key="item.model"
                  :span="item.span || 5"
                >
                  <el-form-item
                    label-width="80px"
                    :label="item.label"
                    class="postInfo-container-item"
                  >
                    <!-- input-->
                    <el-input
                      v-if="item.type == 'input'"
                      v-model="listQuery[item.model]"
                      style="width: 200px"
                      class="filter-item"
                      @keyup.enter.native="item.keyUpEnter"
                    />
                    <!-- select-->
                    <el-select
                      v-if="item.type == 'select'"
                      v-model="listQuery[item.model]"
                      :placeholder="item.placeholder"
                      clearable
                      style="width: 200px"
                      class="filter-item"
                    >
                      <el-option
                        v-for="e in item.option"
                        :key="e.value||e"
                        :label="e.label||e"
                        :value="e.value||e"
                      />
                    </el-select>
                    <!-- 树选择器 需要的参数较多-->
                    <SelectTree
                      v-if="item.type=='selectTree'"
                      :props="item.props"
                      :options="item.optionData"
                      :value="item.valueId"
                      :clearable="item.isClearable"
                      :accordion="item.isAccordion"
                      @getValue="item.getValue($event)"
                      height="200"
                    ></SelectTree>
                    <!--树选择器end -->
                    <!-- 多层级选择器-->
                    <el-cascader
                      v-model="listQuery[item.model]"
                      v-if="item.type == 'cascader'"
                      :options="item.options"
                      :show-all-levels="false"
                      :props="item.props||{ checkStrictly: true }"
                      clearable
                    ></el-cascader>
                    <!-- datepicker-->
                    <el-date-picker
                      v-if="item.type == 'datePicker'"
                      v-model="listQuery[item.model]"
                    />
                    <!-- dateRangepicker -->
                    <el-date-picker
                      v-if="item.type == 'dateRangepicker'"
                      v-model="listQuery[item.model]"
                      type="datetimerange"
                      range-separator="至"
                      start-placeholder="开始日期"
                      end-placeholder="结束日期"
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="6" :offset="2">
                  <el-button
                    v-waves
                    class="filter-item"
                    type="primary"
                    @click="fetchRequest"
                  >
                    新增
                  </el-button>
                  <!-- <el-button
                    v-waves
                    class="filter-item"
                    type="primary"
                    @click="handleReset"
                  >
                    重置
                  </el-button> -->
                </el-col>
              </el-row>
            </div>
          </el-col>
        </el-row>
      </div>
    </el-form>
    <div class="filter-container table-form-header-button"></div>
  </div>
</template>
<script>
// import {  fetchPv, createArticle, updateArticle } from '@/api/article'
// import { fetchList } from '@/api/basicEquipment'
// import waves from '@/directive/waves' // waves directive
// import { parseTime } from '@/utils'
// import Pagination from '@/components/Pagination' // secondary package based on el-pagination
// import {mapGetters} from 'vuex'
// const calendarTypeOptions = [
//   { key: 'CN', display_name: 'China' },
//   { key: 'US', display_name: 'USA' },
//   { key: 'JP', display_name: 'Japan' },
//   { key: 'EU', display_name: 'Eurozone' }
// ]

// // arr to obj, such as { CN : "China", US : "USA" }
// const calendarTypeKeyValue = calendarTypeOptions.reduce((acc, cur) => {
//   acc[cur.key] = cur.display_name
//   return acc
// }, {})
import waves from "@/directive/waves"; // waves directive
import SelectTree from"@/components/SelectTree"
// const calendarTypeOptions = [
//   { key: "CN", display_name: "China" },
//   { key: "US", display_name: "USA" },
//   { key: "JP", display_name: "Japan" },
//   { key: "EU", display_name: "Eurozone" },
// ];
export default {
  name: "ListSearchParams",
  directives: { waves },
  components:{SelectTree},
  props: ["dataModel", "fetchRequest"],
  data() {
    return {
      dataObject: this.dataModel,
      listQuery: this.dataModel,

     // calendarTypeOptions,
      sortOptions: [
        { label: "ID Ascending", key: "+id" },
        { label: "ID Descending", key: "-id" },
      ],
      statusOptions: ["published", "draft", "deleted"],
      showReviewer: false,
      temp: {
        id: undefined,
        importance: 1,
        remark: "",
        timestamp: new Date(),
        title: "",
        type: "",
        status: "published",
      },
      dialogFormVisible: false,
      dialogStatus: "",
      textMap: {
        update: "Edit",
        create: "Create",
      },
      dialogPvVisible: false,
      pvData: [],
      rules: {
        type: [
          { required: true, message: "type is required", trigger: "change" },
        ],
        timestamp: [
          {
            type: "date",
            required: true,
            message: "timestamp is required",
            trigger: "change",
          },
        ],
        title: [
          { required: true, message: "title is required", trigger: "blur" },
        ],
      },
      downloadLoading: false,
    };
  },
  watch: {
    listQuery: {
      handler: function (val) {
        this.$store
          .dispatch("basicEquipment/setListSearchParams", val)
          .then(() => {
            // this.$emit('change')
            console.log(this.$store.state);
          });
      },
      deep: true,
    },
  },
  created() {
    this.listQuery = this.$store.state.basicEquipment.listSearchParams || {}; // 操作状态
  },
  methods: {
    // 搜索方法父组件传递方法过来
    handleFilter() {
      this.fetchRequest(this.listQuery);
    },
    // 重置,本组件自行处理
    handleReset() {
      this.listQuery = {};
      this.fetchRequest(this.listQuery);
    },
  },
};
</script>
